import { Message, Notification } from 'element-ui';
import Clipboard from 'clipboard';
import lang from '@/lang';


function clipboardSuccess() {
  Notification({
    message: lang.t('copy.success'),
    type: 'success',
    duration: 1000,
    showClose: false,
  });
}

function clipboardError() {
  Message.error(lang.t('copy.fail'));
}

export default function handleClipboard(text, event) {
  const clipboard = new Clipboard(event.target, {
    text: () => text,
  });
  clipboard.on('success', () => {
    clipboardSuccess();
    clipboard.off('error');
    clipboard.off('success');
    clipboard.destroy();
  });
  clipboard.on('error', () => {
    clipboardError();
    clipboard.off('error');
    clipboard.off('success');
    clipboard.destroy();
  });
  clipboard.onClick(event);
}
